home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / esc.jar / com / extensibility / xml / XMLReader.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-06-30  |  5.9 KB  |  389 lines

  1. package com.extensibility.xml;
  2.  
  3. import com.extensibility.util.Debug;
  4. import java.io.EOFException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.Reader;
  8. import java.util.Hashtable;
  9.  
  10. public final class XMLReader extends Reader {
  11.    public static final int UNKNOWN = -1;
  12.    public static final int UTF_8 = 0;
  13.    public static final int UTF_16_1_2 = 1;
  14.    public static final int UTF_16_2_1 = 2;
  15.    public static final int ISO_8859_1 = 3;
  16.    public static final int ASCII = 4;
  17.    // $FF: renamed from: in java.io.InputStream
  18.    InputStream field_0;
  19.    byte[] rawBytes = new byte[8080];
  20.    int pos = 0;
  21.    int eob = 0;
  22.    char nextChar;
  23.    int firstShift;
  24.    int secondShift;
  25.    int encoding = -1;
  26.    String encodingAssignment;
  27.  
  28.    XMLReader(InputStream var1) {
  29.       this.field_0 = var1;
  30.    }
  31.  
  32.    private void establishEncoding() throws IOException {
  33.       if (this.encoding == -1) {
  34.          this.encoding = 0;
  35.  
  36.          try {
  37.             byte var1 = this.getByte();
  38.             byte var2 = this.getByte();
  39.             if (var1 == -2 && var2 == -1) {
  40.                this.encoding = 1;
  41.                this.firstShift = 8;
  42.             } else if (var1 == -1 && var2 == -2) {
  43.                this.encoding = 2;
  44.                this.secondShift = 8;
  45.             } else {
  46.                this.pos = 0;
  47.             }
  48.  
  49.             String var3 = this.extractEncodingDecl();
  50.             if (var3 != null) {
  51.                this.setEncoding(var3);
  52.             }
  53.          } catch (EOFException var4) {
  54.             this.pos = 0;
  55.          }
  56.  
  57.       }
  58.    }
  59.  
  60.    private boolean canReadFromBuffer() {
  61.       return this.pos < this.eob - 4;
  62.    }
  63.  
  64.    private boolean peekStartsWith(String var1) {
  65.       int var2 = this.pos;
  66.       int var3 = 0;
  67.       if (this.eob == -1) {
  68.          return false;
  69.       } else {
  70.          try {
  71.             while(this.canReadFromBuffer()) {
  72.                int var4 = ((Reader)this).read();
  73.                if (var4 != var1.charAt(var3)) {
  74.                   boolean var12 = false;
  75.                   return var12;
  76.                }
  77.  
  78.                if (var3 == var1.length() - 1) {
  79.                   boolean var5 = true;
  80.                   return var5;
  81.                }
  82.  
  83.                ++var3;
  84.             }
  85.  
  86.             return false;
  87.          } catch (IOException var10) {
  88.             return false;
  89.          } finally {
  90.             this.pos = var2;
  91.          }
  92.       }
  93.    }
  94.  
  95.    private String peekUntil(String var1) {
  96.       if (this.eob == -1) {
  97.          return null;
  98.       } else {
  99.          int var2 = this.pos;
  100.          int var3 = 0;
  101.          StringBuffer var4 = new StringBuffer();
  102.  
  103.          try {
  104.             while(this.canReadFromBuffer()) {
  105.                int var5 = ((Reader)this).read();
  106.                var4.append((char)var5);
  107.                if (var5 != var1.charAt(var3)) {
  108.                   var3 = 0;
  109.                } else if (var3 != var1.length() - 1) {
  110.                   ++var3;
  111.                } else {
  112.                   String var6 = var4.toString();
  113.                   return var6;
  114.                }
  115.             }
  116.  
  117.             return null;
  118.          } catch (IOException var11) {
  119.             return null;
  120.          } finally {
  121.             this.pos = var2;
  122.          }
  123.       }
  124.    }
  125.  
  126.    String extractEncodingDecl() {
  127.       if (!this.peekStartsWith("<?xml")) {
  128.          return null;
  129.       } else {
  130.          String var1 = this.peekUntil("?>");
  131.  
  132.          try {
  133.             Hashtable var2 = XMLStringUtilities.getNameValuePairs(var1.substring(5, var1.length() - 2));
  134.             String var5 = (String)var2.get("encoding");
  135.             return var5;
  136.          } catch (StringIndexOutOfBoundsException var4) {
  137.             Object var3 = null;
  138.             return (String)var3;
  139.          }
  140.       }
  141.    }
  142.  
  143.    public int getEncoding() {
  144.       try {
  145.          if (this.encoding == -1) {
  146.             this.establishEncoding();
  147.          }
  148.       } catch (IOException var2) {
  149.       }
  150.  
  151.       return this.encoding;
  152.    }
  153.  
  154.    public String getAssignedEncoding() {
  155.       try {
  156.          if (this.encoding == -1) {
  157.             this.establishEncoding();
  158.          }
  159.       } catch (IOException var2) {
  160.       }
  161.  
  162.       return this.encodingAssignment;
  163.    }
  164.  
  165.    public static boolean supportsEncoding(String var0) {
  166.       return var0 == null || var0.equalsIgnoreCase("UTF-8") || var0.equalsIgnoreCase("UTF-16") || var0.equalsIgnoreCase("US-ASCII") || var0.equalsIgnoreCase("ASCII") || var0.equalsIgnoreCase("ISO-8859-1");
  167.    }
  168.  
  169.    private boolean setEncoding(String var1) {
  170.       if (var1 == null) {
  171.          this.encoding = 0;
  172.       } else if (var1.equalsIgnoreCase("UTF-8")) {
  173.          this.encoding = 0;
  174.       } else if (var1.equalsIgnoreCase("US-ASCII")) {
  175.          this.encoding = 4;
  176.       } else if (var1.equalsIgnoreCase("ASCII")) {
  177.          this.encoding = 4;
  178.       } else if (var1.equalsIgnoreCase("ISO-8859-1")) {
  179.          this.encoding = 3;
  180.       } else {
  181.          if (!var1.equalsIgnoreCase("UTF-16")) {
  182.             return false;
  183.          }
  184.  
  185.          Debug.assert(this.encoding == 1 || this.encoding == 2, "unexpected encoding");
  186.       }
  187.  
  188.       this.encodingAssignment = var1;
  189.       return true;
  190.    }
  191.  
  192.    public int read(char[] var1, int var2, int var3) throws IOException {
  193.       this.establishEncoding();
  194.       switch (this.encoding) {
  195.          case 0:
  196.             return this.readUTF8(var1, var2, var3);
  197.          case 1:
  198.          case 2:
  199.             return this.readUTF16(var1, var2, var3);
  200.          case 3:
  201.             return this.readISO_8859_1(var1, var2, var3);
  202.          case 4:
  203.             return this.readASCII(var1, var2, var3);
  204.          default:
  205.             return -1;
  206.       }
  207.    }
  208.  
  209.    public int readUTF8(char[] var1, int var2, int var3) throws IOException {
  210.       int var4 = 0;
  211.  
  212.       while(var3-- > 0) {
  213.          if (this.nextChar != 0) {
  214.             var1[var2++] = this.nextChar;
  215.             this.nextChar = 0;
  216.             ++var4;
  217.          } else {
  218.             byte var5;
  219.             try {
  220.                if (this.pos < this.eob) {
  221.                   var5 = this.rawBytes[this.pos++];
  222.                } else {
  223.                   var5 = this.getByte();
  224.                }
  225.             } catch (EOFException var16) {
  226.                break;
  227.             }
  228.  
  229.             if ((var5 & 128) == 0) {
  230.                var1[var2++] = (char)var5;
  231.             } else if ((var5 & 224) == 192) {
  232.                byte var6;
  233.                try {
  234.                   var6 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
  235.                } catch (EOFException var15) {
  236.                   throw new EncodingException("2-byte character left un-finished.");
  237.                }
  238.  
  239.                if ((var6 & 192) != 128) {
  240.                   throw new EncodingException("invalid second byte");
  241.                }
  242.  
  243.                var1[var2++] = (char)((var5 & 31) << 6 | var6 & 63);
  244.             } else if ((var5 & 240) == 224) {
  245.                byte var17;
  246.                try {
  247.                   var17 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
  248.                } catch (EOFException var14) {
  249.                   throw new EncodingException("3-byte character left un-finished (2).");
  250.                }
  251.  
  252.                if ((var17 & 192) != 128) {
  253.                   throw new EncodingException("invalid second byte");
  254.                }
  255.  
  256.                byte var7;
  257.                try {
  258.                   var7 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
  259.                } catch (EOFException var13) {
  260.                   throw new EncodingException("3-byte character left un-finished (3).");
  261.                }
  262.  
  263.                if ((var7 & 192) != 128) {
  264.                   throw new EncodingException("invalid third byte");
  265.                }
  266.  
  267.                var1[var2++] = (char)((var5 & 15) << 12 | (var17 & 63) << 6 | var7 & 63);
  268.             } else if ((var5 & 248) == 240) {
  269.                byte var18;
  270.                try {
  271.                   var18 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
  272.                } catch (EOFException var12) {
  273.                   throw new EncodingException("4-byte character left un-finished (2).");
  274.                }
  275.  
  276.                if ((var18 & 192) != 128) {
  277.                   throw new EncodingException("invalid second byte");
  278.                }
  279.  
  280.                byte var19;
  281.                try {
  282.                   var19 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
  283.                } catch (EOFException var11) {
  284.                   throw new EncodingException("4-byte character left un-finished (3).");
  285.                }
  286.  
  287.                if ((var19 & 192) != 128) {
  288.                   throw new EncodingException("invalid third byte");
  289.                }
  290.  
  291.                byte var8;
  292.                try {
  293.                   var8 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
  294.                } catch (EOFException var10) {
  295.                   throw new EncodingException("4-byte character left un-finished (4).");
  296.                }
  297.  
  298.                char var9 = (char)(((15 & var5) << 18) + ((63 & var18) << 12) + ((63 & var19) << 6) + (63 & var8));
  299.                if (var9 > 65536) {
  300.                   var1[var2++] = (char)(var9 - 65536 >> 10 | '\ud800');
  301.                   this.nextChar = (char)(var9 - 65536 & 1023 | '\udc00');
  302.                }
  303.             } else {
  304.                var1[var2++] = (char)var5;
  305.             }
  306.  
  307.             ++var4;
  308.          }
  309.       }
  310.  
  311.       return var4 == 0 ? -1 : var4;
  312.    }
  313.  
  314.    public int readUTF16(char[] var1, int var2, int var3) throws IOException {
  315.       int var6;
  316.       for(var6 = 0; var3-- > 0; ++var6) {
  317.          byte var4;
  318.          try {
  319.             var4 = this.getByte();
  320.          } catch (EOFException var9) {
  321.             break;
  322.          }
  323.  
  324.          byte var5;
  325.          try {
  326.             var5 = this.getByte();
  327.          } catch (EOFException var8) {
  328.             throw new EncodingException("An odd number of bytes encountered.");
  329.          }
  330.  
  331.          var1[var2++] = (char)(var4 << this.firstShift | var5 << this.secondShift);
  332.       }
  333.  
  334.       return var6 == 0 ? -1 : var6;
  335.    }
  336.  
  337.    public int readISO_8859_1(char[] var1, int var2, int var3) throws IOException {
  338.       int var5;
  339.       for(var5 = 0; var3-- > 0; ++var5) {
  340.          try {
  341.             byte var4 = this.getByte();
  342.             var1[var2++] = (char)var4;
  343.          } catch (EOFException var7) {
  344.             break;
  345.          }
  346.       }
  347.  
  348.       return var5 == 0 ? -1 : var5;
  349.    }
  350.  
  351.    public int readASCII(char[] var1, int var2, int var3) throws IOException {
  352.       int var5;
  353.       for(var5 = 0; var3-- > 0; ++var5) {
  354.          try {
  355.             byte var4 = this.getByte();
  356.             var1[var2++] = (char)var4;
  357.          } catch (EOFException var7) {
  358.             break;
  359.          }
  360.       }
  361.  
  362.       return var5 == 0 ? -1 : var5;
  363.    }
  364.  
  365.    private byte getByte() throws IOException {
  366.       if (this.pos < this.eob) {
  367.          return this.rawBytes[this.pos++];
  368.       } else if (this.eob == -1) {
  369.          throw new EOFException();
  370.       } else {
  371.          Debug.assert(this.pos == this.eob, String.valueOf(String.valueOf(String.valueOf("pos != eob as expected: ").concat(String.valueOf(this.pos))).concat(String.valueOf(" != "))).concat(String.valueOf(this.eob)));
  372.          this.eob = this.field_0.read(this.rawBytes, 0, this.rawBytes.length);
  373.          if (this.eob == -1) {
  374.             throw new EOFException();
  375.          } else {
  376.             this.pos = 0;
  377.             return this.getByte();
  378.          }
  379.       }
  380.    }
  381.  
  382.    public void close() throws IOException {
  383.       if (this.field_0 != null) {
  384.          this.field_0.close();
  385.          this.field_0 = null;
  386.       }
  387.    }
  388. }
  389.